﻿/******************************************************************************
    myBoard is a port of MacOS X dashboard application for windows.
    Copyright (C) 2007,2008  by Yann ALET
    Email : yann.alet@gmail.com

    myBoard is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    myBoard is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Guide.XMLTV
{
    public class Program
    {
        // Fields
        private List<string> _categories;
        private string _channelId;
        private Credits _credits;
        private DateTime _date;
        private string _description;
        private DateTime _end;
        private int _length;
        private string _OriginalRatingString;
        private DateTime _start;
        private string _subTitle;
        private string _title;
        private Video _video;

        // Methods
        public Program()
        {
        }

        public Program(XmlNode program)
        {
            this._categories = new List<string>();
            this._channelId = program.Attributes["channel"].Value;
            this._start = this.ParseXmlDateTime(program.Attributes["start"].Value);
            if (program.Attributes["stop"] != null)
            {
                this._end = this.ParseXmlDateTime(program.Attributes["stop"].Value);
            }
            else
            {
                this._end = this._start;
            }
            if (program.HasChildNodes)
            {
                foreach (XmlNode node in program.ChildNodes)
                {
                    if (node.Name == "title")
                    {
                        this._title = node.InnerText;
                    }
                    if (node.Name == "sub-title")
                    {
                        this._subTitle = node.InnerText;
                    }
                    if ((node.Name == "desc") && (string.IsNullOrEmpty(this._description) || (node.InnerText.Length > this._description.Length)))
                    {
                        this._description = node.InnerText;
                    }
                    if (node.Name == "category")
                    {
                        this._categories.Add(node.InnerText);
                    }
                    if (node.Name == "length")
                    {
                        this._length = Convert.ToInt16(node.InnerText);
                    }
                    if (node.Name == "date")
                    {
                        if (node.InnerText.Length == 4)
                        {
                            this._date = new DateTime(Convert.ToInt16(node.InnerText), 1, 1);
                        }
                        else
                        {
                            int year = Convert.ToInt16(node.InnerText.Substring(0, 4));
                            int month = Convert.ToInt16(node.InnerText.Substring(4, 2));
                            int day = Convert.ToInt16(node.InnerText.Substring(6, 2));
                            this._date = new DateTime(year, month, day);
                        }
                    }
                    if (node.Name == "star-rating")
                    {
                        foreach (XmlNode node2 in node.ChildNodes)
                        {
                            if (node2.Name == "value")
                            {
                                this._OriginalRatingString = node2.InnerText;
                                break;
                            }
                        }
                    }
                    if (node.Name == "video")
                    {
                        this._video = new Video();
                        foreach (XmlNode node3 in node.ChildNodes)
                        {
                            if (node3.Name == "colour")
                            {
                                this._video.InColor = node3.InnerText == "yes";
                            }
                            if (node3.Name == "aspect")
                            {
                                this._video.Format = node3.InnerText;
                            }
                        }
                    }
                    if (node.Name == "credits")
                    {
                        this._credits = new Credits();
                        foreach (XmlNode node4 in node.ChildNodes)
                        {
                            if (node4.Name == "director")
                            {
                                this._credits.Director = node4.InnerText;
                            }
                            if (node4.Name == "actor")
                            {
                                this._credits.Actors.Add(node4.InnerText);
                            }
                            if (node4.Name == "writer")
                            {
                                this._credits.Writer.Add(node4.InnerText);
                            }
                            if (node4.Name == "presenter")
                            {
                                this._credits.Presenter.Add(node4.InnerText);
                            }
                        }
                    }
                }
            }
        }

        private DateTime ParseXmlDateTime(string dateTime)
        {
            int year = Convert.ToInt16(dateTime.Substring(0, 4));
            int month = Convert.ToInt16(dateTime.Substring(4, 2));
            int day = Convert.ToInt16(dateTime.Substring(6, 2));
            int hour = Convert.ToInt16(dateTime.Substring(8, 2));
            return new DateTime(year, month, day, hour, Convert.ToInt16(dateTime.Substring(10, 2)), 0);
        }

        // Properties
        public List<string> Categories
        {
            get
            {
                return this._categories;
            }
            set
            {
                this._categories = value;
            }
        }

        public string ChannelId
        {
            get
            {
                return this._channelId;
            }
            set
            {
                this._channelId = value;
            }
        }

        public Credits Credits
        {
            get
            {
                return this._credits;
            }
            set
            {
                this._credits = value;
            }
        }

        public DateTime Date
        {
            get
            {
                return this._date;
            }
            set
            {
                this._date = value;
            }
        }

        public string Description
        {
            get
            {
                return this._description;
            }
            set
            {
                this._description = value;
            }
        }

        public DateTime End
        {
            get
            {
                return this._end;
            }
            set
            {
                this._end = value;
            }
        }

        public int Length
        {
            get
            {
                return this._length;
            }
            set
            {
                this._length = value;
            }
        }

        public string LengthFormated
        {
            get
            {
                TimeSpan span = (TimeSpan)(this._end - this._start);
                return string.Format("({0}')", span.TotalMinutes);
            }
        }

        public int MaxRating
        {
            get
            {
                return Convert.ToInt16(this._OriginalRatingString.Split(new char[] { '/' })[1]);
            }
        }

        public string OriginalRatingString
        {
            get
            {
                return this._OriginalRatingString;
            }
            set
            {
                this._OriginalRatingString = value;
            }
        }

        public int Rating
        {
            get
            {
                return Convert.ToInt16(this._OriginalRatingString.Split(new char[] { '/' })[0]);
            }
        }

        public DateTime Start
        {
            get
            {
                return this._start;
            }
            set
            {
                this._start = value;
            }
        }

        public string StartTimeShort
        {
            get
            {
                return this._start.ToShortTimeString();
            }
        }

        public string SubTitle
        {
            get
            {
                return this._subTitle;
            }
            set
            {
                this._subTitle = value;
            }
        }

        public string Title
        {
            get
            {
                return this._title;
            }
            set
            {
                this._title = value;
            }
        }

        public Video Video
        {
            get
            {
                return this._video;
            }
            set
            {
                this._video = value;
            }
        }

    }
}
